//
//  ViewController.m
//  UICollectionViewAdvance
//
//  Created by GuochengLiu on 2016/12/23.
//  Copyright © 2016年 sumavision. All rights reserved.
//

#import "ViewController.h"
#import "ViewModel.h"
#import "ChannelCell.h"
#import "CollectionHeaderView.h"
#import <UIScrollView+MJRefresh.h>
#import <MJRefreshNormalHeader.h>

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, CollectionHeaderViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) ViewModel *viewModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewModel = [[ViewModel alloc] init];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ChannelCell" bundle:nil] forCellWithReuseIdentifier:@"ChannelCell"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderView"];
    
    __weak __typeof__(self) weakSelf = self;
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf reloadContent];
    }];
    
    [self.collectionView.mj_header beginRefreshing];
}

- (void)reloadContent
{
    // 这里ViewModel产生了两个回调，一个是健在栏目成功的回调，一个是加载栏目底下频道成功的回调
    [_viewModel fetchDataWithCompleted:^(ViewModel *model, NSError *error) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView reloadData];
    } sectionCompleted:^(ViewModel *model, LiveColumn *liveColumn, NSInteger section) {
        [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:section]];
    }];
}

#pragma mark - <UICollectionViewDataSource>


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _viewModel.arrayColumn.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    LiveColumn *liveColumn = [_viewModel.arrayColumn objectAtIndex:section];
    return liveColumn.arrayChannel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LiveColumn *liveColumn = [_viewModel.arrayColumn objectAtIndex:indexPath.section];
    NSString *channelName = liveColumn.arrayChannel[indexPath.row];
    ChannelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChannelCell" forIndexPath:indexPath];
    
    cell.channelNameLabel.text = channelName;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    LiveColumn *liveColumn = [_viewModel.arrayColumn objectAtIndex:section];

    if (liveColumn.expand == YES) {
        return CGSizeMake(self.collectionView.frame.size.width, 80);
    } else {
        return CGSizeMake(self.collectionView.frame.size.width, 40);
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    LiveColumn *liveColumn = [_viewModel.arrayColumn objectAtIndex:indexPath.section];

    CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionHeaderView" forIndexPath:indexPath];
    
    [headerView.headerTitlelabel setText:liveColumn.columnName];
    headerView.liveColumn = liveColumn;
    
    [headerView setDelegate:(id<CollectionHeaderViewDelegate>)self];
    return headerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)shouldExpand:(BOOL)expand headerView:(CollectionHeaderView *)headerView
{
    headerView.liveColumn.expand = expand;
    [self.collectionView performBatchUpdates:^{
    } completion:nil];
}



@end
